/******************************************************************************
    XFlightRec
    Copyright (C) 2009 Mike Gehl

    X-Plane Plugin SDK
    Copyright (C) Sandy Barbour and Ben Supnik

    Data conversion routines to NAV3
    Copyright (C) 2000 Ted Wright
    NASA Business: wright@grc.nasa.gov, Remainder: wright@en.com

    This file is part of XFlightRec.

    XFlightRec is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    XFlightRec is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with XFlightRec.  If not, see <http://www.gnu.org/licenses/>.

  *****************************************************************************
  * conversion.c
  * Copyright (C) Mike Gehl 2009 <mjgehl@gmail.com>
  *
  * Convert x-plane datarefs to those that can be read by NAV.
  * For a desciption of the NAV data format, see xflightrec.h.
  */

#include "conversion.h"


void to_time( double seconds, FLTREC_DATA *flt_rec)
{
    flt_rec->wClockHour = (int)seconds / 3600;
    flt_rec->wClockMin  = (int)(seconds / 60) - (flt_rec->wClockHour * 60);
    flt_rec->wClockSec  = (int)seconds % 60;
}


void to_lattitude(double lat, FLTREC_DATA *flt_rec)
{
    lat = lat / LAT_CONVERSION;
    DWORD latHi = lat;
    DWORD latLo;

    if (lat >= 0)
        latLo = (double)0x100000000 * (lat - (double)latHi);
    else
        latLo = (double)0x100000000 * (lat + (double)latHi) * -1.0;

    flt_rec->dwLatHi = latHi;
    flt_rec->dwLatLo = latLo;
}


void to_longitude(double lon, FLTREC_DATA *flt_rec)
{
    lon = lon / LON_CONVERSION;
    DWORD lonHi = lon;
    DWORD lonLo;
    if (lon >= 0)
        lonLo = (double)0x100000000 * (lon - (double)lonHi);
    else
        lonLo = (double)0x100000000 * (lon + (double)lonHi) * -1.0;

    flt_rec->dwLonHi = lonHi;
    flt_rec->dwLonLo = lonLo;
}


void to_altitude(double alt, FLTREC_DATA *flt_rec)
{
    DWORD altHi = alt;
    DWORD altLo = (double)0x100000000 * (alt - (double)altHi);
    flt_rec->dwAltHi = altHi;
    flt_rec->dwAltLo = altLo;
}

DWORD to_orientation(double orient)
{
    return (orient * (double)0x100000000 / 360.0);
}


DWORD to_airspeed(double speed)
{
    return (speed * 256.0 * (DEGREES_TO_METERS/60.0) / 3600.0); //kts
}


double meters_per_sec_to_knots(double speed)
{
    return (speed * METERS_TO_KNOTS);
}


WORD to_gear_pos(double pos)
{
    if (pos > 0)
        return 0xffff;
    return 0;
}
